package com.ydd.service.impl;

import com.ydd.dto.ConfigUpdateDto;
import com.ydd.entity.SysConfig;
import com.ydd.mapper.SysConfigMapper;
import com.ydd.service.SysConfigService;
import com.ydd.vo.SYSConfigAllVO;
import com.ydd.vo.common.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description TODO
 * @Author fang
 * @Date 2023-05-29 16:24
 */
@Service
public class SysConfigServiceImpl implements SysConfigService {
    @Autowired
    SysConfigMapper sysConfigMapper;


    /**
     * 权限管理-获取所有配置数据
     *
     * @return
     */
    @Override
    public SYSConfigAllVO getAllConfig() {
        //configForm
        List<SysConfig> cfList = sysConfigMapper.getConfigForm();
        Map<String, String> kvMap = cfList.stream()
                .collect(Collectors.toMap(SysConfig::getAttrKey, SysConfig::getAttrValue));
        ConfigForm configForm = ConfigForm.builder()
                .system_name(kvMap.get("system_name"))
                .system_logo(kvMap.get("system_logo"))
                .web_ico(kvMap.get("web_ico"))
                .site_name(kvMap.get("site_name"))
                .site_link(kvMap.get("site_link"))
                .site_keywords(kvMap.get("site_keywords"))
                .close_website(kvMap.get("close_website"))
                .copy_code(kvMap.get("copy_code"))
                .copy_info(kvMap.get("copy_info"))
                .copy_url(kvMap.get("copy_url"))
                .black_ip(kvMap.get("black_ip"))
                .login_title(kvMap.get("login_title"))
                .login_bg(kvMap.get("login_bg"))
                .loginreg_bg(kvMap.get("loginreg_bg"))
                .notic_title(kvMap.get("notic_title"))
                .notic_content(kvMap.get("notic_content"))
                .web_img(kvMap.get("web_img"))
                .phone(kvMap.get("phone"))
                .build();

        //configRules
        SiteName siteName = new SiteName(sysConfigMapper.getConfigRules());

        //allDate
        List<AllData> allDateList = getAllDataList();

        //list
        List<ConfigList> list = getConfigLists();

        SYSConfigAllVO sysConfigAllVO = SYSConfigAllVO.builder()
                .list(list)
                .allData(allDateList)
                .configForm(configForm)
                .configRules(siteName)
                .build();

        return sysConfigAllVO;
    }

    //getAllConfigget的辅助方法+复用
    public List<AllData> getAllDataList() {
        List<SysConfig> sysConfigList = sysConfigMapper.getAllConfig(); //全数据
        List<AllData> allDateList = getAllData(sysConfigList);
        return allDateList;
    }

    //getAllConfig的辅助方法
    private List<AllData> getAllData(List<SysConfig> sysConfigList) {
        List<AllData> allDateList = new ArrayList<>();
        for (SysConfig sysConfig : sysConfigList) {
            AllData allData = new AllData();
            BeanUtils.copyProperties(sysConfig, allData);
            allData.setAttr_key(sysConfig.getAttrKey());
            allData.setAttr_value(sysConfig.getAttrValue());
            allData.setCan_delete(sysConfig.getCanDelete());
            allData.setSort_num(sysConfig.getSortNum());
            allData.setGroup_value(sysConfig.getGroupValue());
            allData.setTab_value(sysConfig.getTabValue());
            allData.setAttr_name(sysConfig.getAttrName());
            allData.setDel_flag(sysConfig.getDelFlag());
            List<ConfigValue> valueList = allData.getConfig_value();
            String configValueStr = sysConfig.getConfigValue();
            if (configValueStr != null && configValueStr.length() != 0) {
                String[] split = configValueStr.split("=");
                valueList = new ArrayList<>();
                valueList.add(
                        ConfigValue.builder()
                                .label(split[0])
                                .value(split[1].substring(0, 1))
                                .build()
                );
                valueList.add(
                        ConfigValue.builder()
                                .label(split[1].substring(2))
                                .value(split[2])
                                .build()
                );
                allData.setConfig_value(valueList);
            }
            allDateList.add(allData);
        }
        return allDateList;
    }

    //getAllConfig的辅助方法
    public List<ConfigList> getConfigLists() {
        List<ConfigList> list = new ArrayList<>();
        List<String> tabValues = sysConfigMapper.getTabValue();
        for (String tabValue : tabValues) {
            ConfigList configList = new ConfigList();
            configList.setKey(tabValue);
            String name = null;
            if (tabValue.equals("base")) {
                name = "基础配置";
            } else if (tabValue.equals("system")) {
                name = "系统设置";
            } else {
                name = "其他设置";
            }
            configList.setName(name);

            //children
            List<Children> childrens = new ArrayList<>();
            List<String> groupValues = sysConfigMapper.getGroupValue(tabValue);
            for (String groupValue : groupValues) {
                Children children = new Children();
                children.setName(groupValue);
                List<SysConfig> sysConfigs = sysConfigMapper.getConfigByGroupValue(groupValue);
                List<AllData> data = getAllData(sysConfigs);
                children.setData(data);
                childrens.add(children);
            }
            configList.setChildren(childrens);
            list.add(configList);
        }
        return list;
    }

    /**
     * 权限管理-更新配置信息
     *
     * @param configDTOList
     */
    @Override
    public void update(List<ConfigUpdateDto> configDTOList) {
        for (ConfigUpdateDto configUpdateDto : configDTOList) {
            sysConfigMapper.updateConfig(configUpdateDto);
        }
    }
}
